package cn.whuc.sparkstreaming

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

object Demo_WordCount_nc {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkstreaming_wc")
    val ssc: StreamingContext = new StreamingContext(conf, Seconds(3))

    val DStream1: ReceiverInputDStream[String] = ssc.socketTextStream("localhost", 9988)

    val words: DStream[String] = DStream1.flatMap(_.split(" "))

    val wordToOne: DStream[(String, Int)] = words.map((_, 1))

    val wordCount: DStream[(String, Int)] = wordToOne.reduceByKey(_ + _)

    wordCount.print()

    ssc.start()

    ssc.awaitTermination()
  }
}
